//
//  RBEssenceViewController.m
//  Budejie
//
//  Created by 华杨科技 on 2017/9/19.
//  Copyright © 2017年 饶波. All rights reserved.
//  精华

#import "RBEssenceViewController.h"
#import "RBTitleButton.h"
#import "RBAllViewController.h"
#import "RBVideoViewController.h"
#import "RBVoiceViewController.h"
#import "RBPictureViewController.h"
#import "RBWordViewController.h"


@interface RBEssenceViewController () <UIScrollViewDelegate>

/**当前选中的标题按钮*/
@property (nonatomic, weak) RBTitleButton *selectedTitleButton;

/**标题按钮底部指示器*/
@property (nonatomic, weak) UIView *indicatorView;

/**标题按钮*/
@property (nonatomic, weak) UIView *titleView;
/**scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;


@end

@implementation RBEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setChildVcViewControllers];

    [self setupScrollView];
    
    [self setupTitleView];

    [self addChildVcView]; // 默认已经来就创建全部自控制器。

}

- (void)setupNav
{
    self.view.backgroundColor = RBCommonbgcolor;
    
    //    UIImageView *imageview = [[UIImageView alloc] init];
    //    imageview.image  =[UIImage imageNamed:@"MainTitle"];
    //    self.navigationItem.titleView  = imageview;
    //    
    // 标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];

    // 左边按钮
    UIBarButtonItem *tagItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highlightImage:@"MainTagSubIconClick" target:self action:@selector(tagClickAction)];
    self.navigationItem.leftBarButtonItem = tagItem;
}

- (void)setupScrollView
{
    // 作用:不允许自动调整scrollView的内边距,让scrollView全屏
    self.automaticallyAdjustsScrollViewInsets =  NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor yellowColor];
    scrollView.frame = self.view.bounds;
    scrollView.pagingEnabled = YES;
//    scrollView.showsVerticalScrollIndicator = NO;
//    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    NSUInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * scrollView.rb_width, 0);  //禁止scrollView上下滚动
//    // 添加所有自控制的view到scrollView
//    NSUInteger count = self.childViewControllers.count;
//    for (int i =0; i < count; i++) {
//        UITableView *childVcView = (UITableView *)self.childViewControllers[i].view;
//        childVcView.backgroundColor = RBRandomColor;
//        childVcView.rb_x = i * childVcView.rb_width;
//        // 让tableView占据整个屏幕
//        childVcView.rb_y = 0;
//        childVcView.rb_height = scrollView.rb_height;
//        
//       // {{0, 0}, {320, 568}}    // {{0, 20}, {320, 548}}
////        NSLog(@"%@",NSStringFromCGRect(childVcView.frame));
//        [scrollView addSubview:childVcView];  
//        
//        // 设置tableView的内边距:解决tableView内容被挡问题
//        childVcView.contentInset = UIEdgeInsetsMake(64+35, 0, 49, 0); 
//        // 设置滚动条的内边距
//        childVcView.scrollIndicatorInsets = childVcView.contentInset;
//    }
//    
//    scrollView.contentSize = CGSizeMake(count * scrollView.rb_width, 0);  //禁止scrollView上下滚动
    
//    NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));
}

- (void)setupTitleView
{
    // 标题栏
    UIView *titleView = [[UIView alloc] init];
//    titleView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
//    titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.2];
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    
    titleView.frame = CGRectMake(0, 64, self.view.rb_width, 35);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    NSArray *titleArr = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat btnW = titleView.rb_width / titleArr.count;
    CGFloat btnH = titleView.rb_height;
    // 添加标题
    for (NSUInteger i =0; i <5 ; i++) {
        // 创建
        RBTitleButton *titleButton = [RBTitleButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        [titleView addSubview:titleButton];
   
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        // 设置标题
        [titleButton setTitle:titleArr[i] forState:(UIControlStateNormal)];
        
        // frame
        titleButton.frame = CGRectMake(i*btnW, 0, btnW, btnH);
    }
    
    // 按钮的选中颜色
    RBTitleButton *firstTitleButton = titleView.subviews.firstObject;
    
    // 底部指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:(UIControlStateSelected)];
    indicatorView.rb_height = 1;
    indicatorView.rb_y = titleView.rb_height - indicatorView.rb_height;
    [titleView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 提前计算好按钮label长度
    [firstTitleButton.titleLabel sizeToFit];
    self.indicatorView.rb_width = firstTitleButton.titleLabel.rb_width; // 指示器宽度=btn.titleLabel的宽度
    self.indicatorView.rb_center_x = firstTitleButton.rb_center_x;

    // 设置默认选中按钮
    [self titleClick:firstTitleButton];
}

- (void)titleClick:(RBTitleButton *)btn
{      
    self.selectedTitleButton.selected = NO;
    btn.selected = YES;
    self.selectedTitleButton = btn;
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.rb_width = btn.titleLabel.rb_width; // 指示器宽度=btn.titleLabel的宽度
        self.indicatorView.rb_center_x = btn.rb_center_x;
    }];
    
    
    // 让scrollView滚动到对应的位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x  = btn.tag * _scrollView.rb_width;
    [self.scrollView setContentOffset:offset animated:YES];
  
//    self.selectedTitleButton.enabled = YES;
//    btn.enabled = NO;
//    self.selectedTitleButton = btn;
    
//    [self.selectedTitleButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    // 修改文字颜色
//    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    self.selectedTitleButton = btn;
}

- (void)setChildVcViewControllers
{
    RBAllViewController *allVC = [[RBAllViewController alloc] init];
    [self addChildViewController:allVC];

    RBVideoViewController *radio = [[RBVideoViewController alloc] init];
    [self addChildViewController:radio];

    RBVoiceViewController *voice = [[RBVoiceViewController alloc] init];
    [self addChildViewController:voice];

    RBPictureViewController *picture = [[RBPictureViewController alloc] init];
    [self addChildViewController:picture];

    RBWordViewController *word = [[RBWordViewController alloc] init];
    [self addChildViewController:word];
}

- (void)tagClickAction {
    RBNSLogFunction;
}

#pragma mark - 添加子控制器view到scrollView上
- (void)addChildVcView
{  
    // 子控制器索引 index
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;

    // 取出子控制器
    UIViewController *childVc = self.childViewControllers[index];

    // 优化代码
//    if (childVc.view.superview) return; // 如果supView有值，说明已经被添加到superView上了,就不重复添加
//    if (childVc.view.window) return; // 如果window有值，说明已经被加到window上
    if ([childVc  isViewLoaded]) return; //
     
//    childVc.view.rb_x = index *self.scrollView.rb_width;
//    childVc.view.rb_y = 0;
//    childVc.view.rb_width = self.scrollView.rb_width;
//    childVc.view.rb_height = self.scrollView.rb_height;
    
//    childVc.view.rb_x = self.scrollView.contentOffset.x;
//    childVc.view.rb_y = self.scrollView.bounds.origin.y;
//    childVc.view.rb_width = self.scrollView.bounds.size.width;
//    childVc.view.rb_height = self.scrollView.bounds.size.height;
    
    childVc.view.frame = self.scrollView.bounds;
    
    [self.scrollView addSubview:childVc.view];
    
    // 结论scrollView的bounds.origin = scrollView.contentOffset
//    NSLog(@"%@--%@",NSStringFromCGPoint(self.scrollView.bounds.origin),NSStringFromCGPoint(self.scrollView.contentOffset));
}

#pragma mark - <UIScrollViewDelegate>
/**
 * 让scrollView产生滚动动画,动画结束的时候回调用这个方法
 * 前提:必须使用setContentOffset:animated方法 或者 scrollRectVisible:animated:方法
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
      
    [self addChildVcView];
}

/**
 * 人为拖拽,↔️左右滑动,会触发这个方法
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    RBNSLogFunction;
    
    NSUInteger index = scrollView.contentOffset.x / scrollView.rb_width;
    
    // 左右拖拽,更新顶部标题选中状态
    RBTitleButton *titleButton = self.titleView.subviews[index];
  
    // 默认情况下:所有view的tag都是0
    // 当tag==0时,viewWithTag 方法返回的就是 self.titleView
//    RBTitleButton *titleButton = [self.titleView viewWithTag:0];
    
    [self titleClick:titleButton];
    
    // 子控制器view添加到scrollView上
    [self addChildVcView];
}

#pragma mark - viewWithTag:
/*
@implementation
- (UIView) viewWithTag:(NSUInteger)tag
{
    if (self.tag == 0) return self;
    
    for (UIView *subView in self.subViews) {
        return [self viewWithTag:tag];
    }
}
@end
 */
#pragma mark - 感受
/**
 */


@end
